package m.emojicreater;

import java.io.File;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;

public class MainActivity extends Activity implements PlatformActionListener {
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ShareSDK.initSDK(this);
		
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		intent.setType("image/*");
		startActivityForResult(intent, 1);
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK) {
			if (data == null) {
				finish();
			} else {
				String path = cn.sharesdk.framework.utils.R.contentUriToPath(this, data.getData());
				if (path == null || path.length() <= 0 || !new File(path).exists()) {
					finish();
				} else {
					Platform plat = ShareSDK.getPlatform(Wechat.NAME);
					ShareParams sp = new ShareParams();
					sp.setShareType(Platform.SHARE_EMOJI);
					sp.setImagePath(path);
					sp.setTitle("wechatemojicreater");
					plat.setPlatformActionListener(this);
					plat.share(sp);
				}
			}
		}
	}
	
	public void onCancel(Platform arg0, int arg1) {
		runOnUiThread(new Runnable() {
			public void run() {
				finish();
			}
		});
	}
	
	public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
		runOnUiThread(new Runnable() {
			public void run() {
				finish();
			}
		});
	}
	
	public void onError(Platform arg0, int arg1, Throwable arg2) {
		runOnUiThread(new Runnable() {
			public void run() {
				finish();
			}
		});
	}
	
}
